import { defineStore } from 'pinia';
import { userLogin } from '../api/zzy'
import { AddressType, UserType } from '../types/zzy_types/user'
import { addressAdd, addressDel, addressGet, addressPut } from '../api/orderDel'

interface UserState {
    userInfo: UserType | null;
}
//用户信息
export const useUserStore = defineStore("user-store", {
    state: (): UserState => ({
        userInfo: null,
    }),
    actions: {
        //带异步请求的action
        async userLoginAction(user: UserType) {
            let res = await userLogin(user);
            this.userInfo = res.data;
            localStorage.setItem("userInfo", JSON.stringify(res.data));
        }
    }
})
interface AddressState {
    list: Array<AddressType>;
    chosenId: string;
}
//地址管理
export const useAddress = defineStore('address', {
    state: (): AddressState => ({
        list: [
            // {
            //     id: '1',
            //     name: '张三',
            //     tel: '13000000000',
            //     address: '浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室',
            //     isDefault: true,
            //     userid: "1",
            //     objectId: "1",
            //     areaCode: "1",
            //     province: "1",
            //     county: "1",
            //     city: "1",
            //     addressDetail: "1",
            //     postalCode: "1",
            //     createdAt: "1"
            // },
            // {
            //     id: '1',
            //     name: '李四',
            //     tel: '1310000000',
            //     address: '浙江省杭州市拱墅区莫干山路 50 号',
            //     userid: "1",
            //     objectId: "1",
            //     areaCode: "1",
            //     province: "1",
            //     county: "1",
            //     city: "1",
            //     addressDetail: "1",
            //     postalCode: "1",
            //     createdAt: "1",
            //     isDefault: false
            // },
        ],
        chosenId: "1"
    }),
    actions: {
        //向数据库增加地址
        async addressAddAction(addressObj: AddressType) {
            let idd = addressObj.objectId
            let res = await addressAdd({ ...addressObj, id: idd })
            let { province, city, county, addressDetail } = addressObj;
            addressObj.address = province + city + county + addressDetail;
            this.list.push(addressObj);
            console.log(res)
        },
        //初始化地址
        async addressGetAction() {
            let res = await addressGet()
            let { results } = res.data
            results.forEach((item: AddressType) => {
                let { province, county, city, addressDetail } = item
                item.id = "parseInt(item.objectId)",
                    item.address = province + county + city + addressDetail
            })
            this.list = results
        },

        //更新编辑地址
        async addressUpdAction(ObjectId: string, addressObj: AddressType) {
            let res = await addressPut(ObjectId, addressObj)
        },

        //删除地址
        async addressDelAction(ObjectId: string) {
            let res = await addressDel(ObjectId)
        }
    }
})